package bb.lanxing.utils;

import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import bb.lanxing.activity.interfaces.Separators;

public class DateUtil {
    public static SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    public static SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    public static SimpleDateFormat format2 = new SimpleDateFormat("H:mm:ss", Locale.getDefault());
    public static SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
    public static SimpleDateFormat format5 = new SimpleDateFormat("mm:ss", Locale.getDefault());
    public static SimpleDateFormat format6 = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    public static SimpleDateFormat format8 = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
    public static SimpleDateFormat format10 = new SimpleDateFormat("MM月dd日 HH:mm", Locale.getDefault());
    public static SimpleDateFormat format11 = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
    public static SimpleDateFormat format12 = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault());
    public static SimpleDateFormat format13 = new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());
    public static SimpleDateFormat format14 = new SimpleDateFormat("yy/MM/dd", Locale.getDefault());
    public static SimpleDateFormat format15 = new SimpleDateFormat("MM-dd", Locale.getDefault());
    public static SimpleDateFormat format16 = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());

    public static SimpleDateFormat format17 = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.getDefault());
    public static TimeZone timeZone = TimeZone.getTimeZone("GMT+8");

    public static String format(long j) {
        return format(j, 0);
    }

    public static String format(long j, int i) {
        return switch (i) {
            case 0 -> format0.format(new Date(j));
            case 1 -> format1.format(new Date(j));
            case 2 -> getHMS(j);
            case 3 -> format3.format(new Date(j));
            case 4 -> getHM(j);
            case 5 -> format5.format(new Date(j));
            case 6 -> format6.format(new Date(j));
            case 7 -> format2.format(new Date(j));
            case 8 -> format8.format(new Date(j));
            case 9 -> format0.format(new Date(j + timeZone.getOffset(0L)));
            case 10 -> format10.format(new Date(j));
            case 11 -> format11.format(new Date(j));
            case 12 -> format12.format(new Date(j));
            case 13 -> format6.format(new Date(j + timeZone.getOffset(0L)));
            case 14 -> format13.format(new Date(j));
            case 15 -> format14.format(new Date(j));
            case 16 -> format15.format(new Date(j));
            case 17 -> format17.format(new Date(j));
            default -> format0.format(new Date(j));
        };
    }

    private static String getHMS(long j) {
        long j2 = j / 1000;
        long j3 = j2 % 60;
        long j4 = j2 / 60;
        long j5 = j4 % 60;
        long j6 = j4 / 60;
        StringBuilder sb = new StringBuilder();
        if (j6 >= 10) {
            sb.append(j6);
        } else if (j6 > 0) {
            sb.append("0");
            sb.append(j6);
        } else {
            sb.append("00");
        }
        sb.append(Separators.COLON);
        if (j5 < 10) {
            sb.append(0);
            sb.append(j5);
        } else {
            sb.append(j5);
        }
        sb.append(Separators.COLON);
        if (j3 < 10) {
            sb.append(0);
            sb.append(j3);
        } else {
            sb.append(j3);
        }
        return sb.toString();
    }

    private static String getHM(long j) {
        long j2 = (j / 1000) / 60;
        long j3 = j2 % 60;
        long j4 = j2 / 60;
        StringBuilder sb = new StringBuilder();
        if (j4 >= 10) {
            sb.append(j4);
        } else if (j4 > 0) {
            sb.append(0);
            sb.append(j4);
        } else {
            sb.append("00");
        }
        sb.append(Separators.COLON);
        if (j3 < 10) {
            sb.append(0);
            sb.append(j3);
        } else {
            sb.append(j3);
        }
        return sb.toString();
    }

    public static long parse(String str) {
        return parse(str, 0);
    }

    public static long parse(String str, int i) {
        try {
            if (i != 0) {
                if (i != 1) {
                    if (i != 2) {
                        if (i != 3) {
                            if (i == 16) {
                                return format16.parse(str).getTime();
                            }
                            return format0.parse(str).getTime();
                        }
                        return format3.parse(str).getTime();
                    }
                    return format2.parse(str).getTime();
                }
                return format1.parse(str).getTime();
            }
            return format0.parse(str).getTime();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    public static void date(TextView textView, long j) {
        textView.setText(DateUtil.format(j * 1000, 1));
    }
}